package servlet;

import bean.MerType;
import com.github.pagehelper.PageInfo;
import dao.impl.MerTypeImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/MerTypeServlet")
public class MerTypeServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //防止中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //获取页面传来的数据
        String method=request.getParameter("method");

        if(method.equals("MerTypeAdd")){
            String whName = request.getParameter("typename");

            int insertMerType = new MerTypeImpl().MerTypeAdd(whName);
            if(insertMerType==1){
                //重新加载 重定向
                response.sendRedirect("MerTypeServlet?method=MerTypeSelect");
            }
        }else if(method.equals("MerTypeins")){
            request.getRequestDispatcher("/MerType.jsp").forward(request,response);
        }else if (method.equals("MerTypeDel")){
            int delId = Integer.parseInt(request.getParameter("whId"));
            int delMerType = new MerTypeImpl().MerTypeDel(delId);

            if(delMerType==1){
                response.sendRedirect("MerTypeServlet?method=MerTypeSelect");
            }
        }else if (method.equals("MerTypeSignal")){
            int whId = Integer.parseInt(request.getParameter("whId"));
            MerType whSignl= new MerTypeImpl().MerTypeSingal(whId);
            System.out.println(whSignl);

            request.setAttribute("whSignl",whSignl);
            request.getRequestDispatcher("/MerTypeUpdate.jsp").forward(request,response);

        }else if (method.equals("MerTypeOne")){
            String whName = request.getParameter("whName");
            PageInfo whOne= new MerTypeImpl().MerTypeOne(whName);
            System.out.println(whOne);

            request.setAttribute("whList",whOne);
            request.getRequestDispatcher("/MerTypeSelect.jsp").forward(request,response);


        }else if (method.equals("MerTypeUpdate")){
            int whId = Integer.parseInt(request.getParameter("whId"));
            System.out.println(whId);
            String whName = request.getParameter("typename");

            int upaMerType = new MerTypeImpl().MerTypeChange(whId,whName);

            if(upaMerType==1){
                response.sendRedirect("MerTypeServlet?method=MerTypeSelect");
            }
        }else if (method.equals("MerTypeSelect")){
            PageInfo pageInfo = new MerTypeImpl().whList(1, 5);
            System.out.println(pageInfo);
            request.setAttribute("whList",pageInfo);
            request.getRequestDispatcher("/MerTypeSelect.jsp").forward(request,response);

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
